// pages/editinfo/editinfo.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    sexs: ['男', '女'],
    indexSex: 0,
    target_step: [8000,16000,24000],
    indexTar: 0,
    tar_step: 8000,
    work_type: ['卧床','轻体力','中体力','重体力'],
    indexwork: 1,
    work_def: '轻体力',
    date: '1994-06-30',
    heights: [],
    weights: [],
    indexHeight: 175,
    indexWeight: 60,
    playuser:[],
    sex:'',
    birthday:'',
    heightt:'',
    weightt:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let height = new Array(220);
    let weight = new Array(120);
    for (let i = 140; i < height.length; i++) {
      height[i] = i + 'CM';
    }
    console.log(height)

    for (let i = 30; i < weight.length; i++) {
      weight[i] = i + 'KG';
    }
    console.log(weight)
    this.setData({
      userinfo: app.globalData.userInfo,
      heights: height,
      weights: weight
    })
    this.getuser();
  },
  getuser:function(){
    var that=this;
      wx.request({
      url: app.d.ceshiUrl + '/Api/User/userinfo',
      method: 'post',
      data: {
        uid: app.d.userId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
           var data=res.data.playuser
           console.log(data);
           console.log(res);
           if(data!=0){
               that.setData({
                  sex: data.sex,
                  birthday: data.birthday,
                  heightt: data.height,
                  weightt: data.weight,
                  work_def:data.work_type
                })
             }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络不给力！',
          duration: 2000
        });
      },
    })
  },
  tijiao:function(){
    var that=this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Step/playuser_edit',
      method: 'post',
      data: {
        uid: app.d.userId,
        sex: that.data.sex,
        birthday: that.data.birthday,
        height: that.data.heightt,
        weight: that.data.weightt,
        target_step:that.data.tar_step,
        work_type:that.data.work_def,
        flag:1
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
          var data=res.data;
          console.log(res);
          if(data.status==1){
            wx.showToast({
              title: '修改成功！',
              duration: 1000
            });
            setTimeout(function () {
                wx.redirectTo({
                       url: '../step/index'
                  });
                }
                  , 1000)
          }else{
               wx.showToast({
                  title: data.err,
                  duration: 2000
                });
          }
    
      },
      fail: function (e) {
        wx.showToast({
          title: '网络不给力！',
          duration: 2000
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindAgeChange: function (e) {
    this.setData({
      sex: this.data.sexs[e.detail.value],
    })
  },
  bindtarChange: function (e) {
    this.setData({
      tar_step: this.data.target_step[e.detail.value],
    })
  },
  bindworkChange: function (e) {
    this.setData({
      work_def: this.data.work_type[e.detail.value],
    })
  },
  bindDateChange: function (e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  bindHeightChange: function (e) {
    this.setData({
      heightt: e.detail.value+ 'CM',
    })
  },
  bindWeightChange: function (e) {
    this.setData({
      weightt: e.detail.value+ 'KG',
    })
  },
})